/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package clientServlets;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.UserHome;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import pojo.Mobile;
import util.HibernateUtil;

/**
 *
 * @author ahmed
 */
public class ShowCart extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        PrintWriter out = null;
        try {
            UserHome userDao = HibernateUtil.getUserDAO();
            if (request.getHeader("User-Agent").contains("Android")) {
                response.setContentType("application/json");
                out = response.getWriter();

                Iterator itr = userDao.getProducts(Integer.parseInt(request.getParameter("userId"))).iterator();

                List<Mobile> mobiles = new ArrayList<Mobile>();
                Mobile newMobile;
                while (itr.hasNext()) {
                    Object[] objArr = (Object[]) itr.next();
                    newMobile = new Mobile();
                    newMobile.setId((Integer) objArr[0]);
                    newMobile.setName((String) objArr[1]);
                    newMobile.setPrice((Integer) objArr[2]);
//                newMobile.setCamera(userDao.getUserById(Integer.parseInt(request.getParameter("userId"))).getCreditBalance());
                    mobiles.add(newMobile);
                }
                Gson gson = new GsonBuilder().create();
                String jsonRepresentation = gson.toJson(mobiles);
                out.print(jsonRepresentation);
            } else {
                response.setContentType("text/html;charset=UTF-8");
                out = response.getWriter();
                Integer userId = (Integer) request.getSession(false).getAttribute("userId");
                Iterator itr = userDao.getProducts(userId).iterator();
                List<Mobile> mobiles = new ArrayList<Mobile>();
                Mobile newMobile;
                while (itr.hasNext()) {
                    Object[] objArr = (Object[]) itr.next();
                    newMobile = new Mobile();
                    newMobile.setId((Integer) objArr[0]);
                    newMobile.setName((String) objArr[1]);
                    newMobile.setPrice((Integer) objArr[2]);
//                newMobile.setCamera(userDao.getUserById(Integer.parseInt(request.getParameter("userId"))).getCreditBalance());
                    mobiles.add(newMobile);
                }

                request.setAttribute("cart", mobiles);
                getServletContext().getRequestDispatcher("/cart.jsp").include(request, response);
            }
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

// parse products from this user to a json string
